//
//  ZJWebViewController.m
//  ShangHaiHuDongApp
//
//  Created by ZJ on 2017/6/23.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "ZJWebViewController.h"
#import <JavaScriptCore/JavaScriptCore.h>
@interface ZJWebViewController ()<UIWebViewDelegate>
/** webView */
@property (nonatomic, strong) UIWebView *webView;
@end

@implementation ZJWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.titlelabel.text = self.titleString;
    [self.view addSubview:self.webView];
}



- (void)action_leftButton:(UIButton *)sender{
    if ([self.webView canGoBack]) {
        [self.webView goBack];
    }else{
        if (self.isModel) {
            [self.navigationController dismissViewControllerAnimated:YES completion:nil];
        }else{
            [self.navigationController popViewControllerAnimated:YES];
        }
    }
}
#pragma mark - UIWebView 协议方法
- (void)webViewDidFinishLoad:(UIWebView *)webView{

    [YJProgressHUD hide];
    if ([self.titleString isEqualToString:@"我的积分"]) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        //js代码
        NSString *jsString = @"var fxd =document.getElementById('fxd'); fxd.onclick = function(){go();} ";
        
        __weak typeof(self) weakSelf = self;
        [webView stringByEvaluatingJavaScriptFromString:jsString];
        //其中 openImage 是JS的函数名，得到的 arg数组 里面为JS的 passValue 函数的参数，即 JS要传给OC的参数。
        JSContext *context = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    
        context[@"go"] = ^{
            [weakSelf.navigationController popViewControllerAnimated:YES];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                [[NSNotificationCenter defaultCenter] postNotificationName:SHAREMORE object:nil];
            });
        };
    });
        
    }
}


- (void)webViewDidStartLoad:(UIWebView *)webView{

    //    [YJProgressHUD hide];
}
#pragma mark - Setter


- (void)setUrlString:(NSString *)urlString{
    _urlString = urlString;
    NSURLRequest *webRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]
                                                cachePolicy:NSURLRequestReturnCacheDataElseLoad
                                            timeoutInterval:30];
    [self.webView loadRequest:webRequest];
     [YJProgressHUD showMessage:@"加载中..." duration:10 view:self.view];
}

#pragma mark - Getters

- (UIWebView *)webView{
    if (_webView == nil){
        _webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, LP_TopHeight, [UIScreen zj_screenWith], [UIScreen zj_screenHeight] - LP_TopHeight )];
        _webView.delegate = self;
        _webView.dataDetectorTypes = UIDataDetectorTypePhoneNumber | UIDataDetectorTypeLink | UIDataDetectorTypeShipmentTrackingNumber | UIDataDetectorTypeFlightNumber | UIDataDetectorTypeLookupSuggestion;
        _webView.backgroundColor = [UIColor clearColor];
        [_webView setOpaque:YES];
    }
    return _webView;
}

@end
